package com.asd.map.controller;

import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.asd.common.Result;
import com.asd.config.IdGenerator;
import com.asd.map.entity.Point_position;
import com.asd.map.entity.vo.Point_positionVo;
import com.asd.map.service.IPoint_positionService;
import com.asd.system.entity.User;
import com.asd.system.service.IImagesService;
import com.asd.util.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 点位控制器
 *
 * @author
 * @createtime 2021-09-02 8:49
 * @description
 */

@Api(tags = "点位Api")
@RestController
@RequestMapping("/api/point")
public class Point_positionController {

    @Autowired
    private IPoint_positionService point_positionService;

    @Autowired
    private IdGenerator idGenerator;


    @Autowired
    private IImagesService imagesService;

    /**
     * 分页列表查询
     *
     * @param
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "点位-分页列表查询", notes = "点位-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "5") Integer pageSize) {


        Page<Point_position> page = new Page<Point_position>(pageNo, pageSize);
        List<Point_position> list = point_positionService.list();
//        Page<Point_position> pageList = page.setRecords(point_positionService.list());


        IPage<Point_position> pageList = point_positionService.page(page);

        return Result.ok(pageList);
    }

    /**
     * 分页模糊查询
     *
     * @param
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "点位-分页模糊查询", notes = "点位-分页模糊查询")
    @GetMapping(value = "/list1")
    public Result<?> queryPageList1(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "5") Integer pageSize,
                                   Point_position point_position) {


        Page<Point_position> page = new Page<Point_position>(pageNo, pageSize);

        IPage<Point_position> pageList = point_positionService.selectPage(page, point_position);

        return Result.ok(pageList);
    }




    /**
     * 新增点位信息
     *
     * @param lng
     * @param lat
     * @param data
     * @param did
     * @return
     */
    @ApiOperation(value = "点位-添加", notes = "点位-添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lng", value = "经度", required = true),
            @ApiImplicitParam(name = "lat", value = "纬度", required = true),
            @ApiImplicitParam(name = "data", value = "基本信息", required = true),
            @ApiImplicitParam(name = "did", value = "数据源id", required = true)
    })
    @PostMapping(value = "/addPoint")
    public Result<?> add1(@RequestParam("lng") String lng, @RequestParam("lat") String lat,
                          @RequestParam("data") String data, @RequestParam("did") String did) {

        Point_position pointPosition = new Point_position();


        pointPosition.setData(data);
        pointPosition.setLat(lat);
        pointPosition.setLng(lng);
        pointPosition.setDid(did);


        point_positionService.save(pointPosition);
//        System.out.println("pointPosition===" + pointPosition);
        return Result.ok(pointPosition);
    }


    /**
     * @param lng
     * @param lat
     * @param data
     * @param did
     * @return
     */

    @ApiOperation(value = "新增点位信息带图片", notes = "新增点位信息带图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lng", value = "经度", required = true),
            @ApiImplicitParam(name = "lat", value = "纬度", required = true),
            @ApiImplicitParam(name = "data", value = "基本信息", required = true),
            @ApiImplicitParam(name = "did", value = "基本信息", required = true),
            @ApiImplicitParam(name = "primaryEchoPath", value = "主图片回显地址"),
            @ApiImplicitParam(name = "viceEchoPathlist", value = "副主图片回显地址")
    })
    @PostMapping(value = "/addPoint1")
    public Result<?> add(@RequestParam("lng") String lng, @RequestParam("lat") String lat,
                         @RequestParam("data") String data, @RequestParam("did") String did,
                         @RequestParam("primaryEchoPath") String primaryEchoPath,
                         @RequestParam("viceEchoPathlist") List<String> viceEchoPathlist) {


        Point_position pointPosition = new Point_position();

        String id = idGenerator.nextId();

        pointPosition.setId(id);
        pointPosition.setData(data);
        pointPosition.setLat(lat);
        pointPosition.setLng(lng);
        pointPosition.setDid(did);
        point_positionService.save(pointPosition);

        if (!(primaryEchoPath.equals(null) || primaryEchoPath.equals(""))) {
            imagesService.updateByPrimaryEchoPath(id, primaryEchoPath);
        }

        if (viceEchoPathlist.size() > 0) {
            for (int i = 0; i < viceEchoPathlist.size(); i++) {
                String viceEchoPath = viceEchoPathlist.get(i);
                imagesService.updateByViceEchoPath(id, viceEchoPath);
            }
        }

        Point_positionVo point_positionVo = point_positionService.findById(id);

        return Result.ok(point_positionVo);
    }

    /**
     * 编辑
     *
     * @param
     * @return
     */
    @ApiOperation(value = "点位-编辑", notes = "点位-编辑")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true),
            @ApiImplicitParam(name = "lng", value = "经度"),
            @ApiImplicitParam(name = "lat", value = "纬度 "),
            @ApiImplicitParam(name = "data", value = "基本信息"),
            @ApiImplicitParam(name = "primaryEchoPath", value = "主图片回显地址"),
            @ApiImplicitParam(name = "viceEchoPathlist", value = "副主图片回显地址")
    })
    @PutMapping(value = "/update")
    public Result<?> edit(@RequestParam("id") String id, @RequestParam("lng") String lng,
                          @RequestParam("lat") String lat, @RequestParam("data") String data,
                          @RequestParam("primaryEchoPath") String primaryEchoPath,
                          @RequestParam("viceEchoPathlist") List<String> viceEchoPathlist) {

        Point_position position = point_positionService.getById(id);

        if (!(lng.equals(null) || lng.equals(""))) {
            position.setLng(lng);
        }
        if (!(lat.equals(null) || lat.equals(""))) {
            position.setLat(lat);
        }
        if (!(data.equals(null) || data.equals(""))) {
            position.setData(data);
        }
        if (!(primaryEchoPath.equals(null) || primaryEchoPath.equals(""))) {
            imagesService.updateByPrimaryEchoPath(id, primaryEchoPath);
        }
        if (viceEchoPathlist.size() > 0) {
            for (int i = 0; i < viceEchoPathlist.size(); i++) {
                String viceEchoPath = viceEchoPathlist.get(i);
                imagesService.updateByViceEchoPath(id, viceEchoPath);
            }
        }

        point_positionService.updateById(position);

        Point_positionVo point_positionVo = point_positionService.findById(id);

        return Result.ok(point_positionVo);

    }


    @ApiOperation(value = "根据did分页查询数据", notes = "根据did分页查询数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pagenum", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小"),
            @ApiImplicitParam(name = "did", value = "数据源id ", required = true),
    })
    @GetMapping("/findByDid")
    public Result<?> findByDid(@RequestParam(name = "pagenum", defaultValue = "1") Integer pagenum,
                               @RequestParam(name = "pageSize", defaultValue = "5") Integer pageSize,
                               @RequestParam(name = "did", required = true) String did) {

        Page<Point_positionVo> page = new Page<>(pagenum, pageSize);

        List<Point_positionVo> point_positionVoList = point_positionService.findByDid1(page, did);

        Page<Point_positionVo> pageList = page.setRecords(point_positionVoList);

        return Result.ok(pageList);
    }


    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "点位-通过id删除", notes = "点位-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {

        point_positionService.removeById(id);


        return Result.ok("删除成功!");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            MultipartFile file = entity.getValue();// 获取上传文件对象
            ImportParams params = new ImportParams();
            params.setTitleRows(0);
            params.setHeadRows(1);
            params.setNeedSave(true);

            try {

                List<Point_position> list = FileUtil.importExcel(file, 0, 1, Point_position.class);

                point_positionService.insertBatch(list);
                return Result.ok("文件导入成功！数据行数：" + list.size());
            } catch (Exception e) {
                return Result.error("文件导入失败:" + e.getMessage());
            } finally {
                try {
                    file.getInputStream().close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return Result.error("文件导入失败！");
    }

    /**
     * 点位二次修改
     */
    @ApiOperation(value = "点位-二次修改", notes = "点位-二次修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "点位id", required = true),
            @ApiImplicitParam(name = "lng", value = "经度", required = true),
            @ApiImplicitParam(name = "lat", value = "纬度 ", required = true)
    })
    @PutMapping("/updatePointModification")
    public Result<?> updatePointModification(@RequestParam("id") String id, @RequestParam("lng") String lng, @RequestParam("lat") String lat) {

        Result<User> result = new Result<User>();

        Point_position pointPosition = point_positionService.getById(id);
        if (pointPosition == null) {
            result.error500("未找到对应点位");
        } else {
            boolean ok = point_positionService.updatePointModification(id, lng, lat);

            if (ok) {
                result.success("修改成功!");
            }

        }
        return result;
    }


}
